import { Toast } from 'vant';
import { createRouter, createWebHistory } from 'vue-router'



function beforeEnter(to, from, next) {
  let isLogin = localStorage.getItem("P4_userId")
  if (isLogin) {
    next();
  } else {
    next('/Login');
  }
}

function isLogin(to, from, next) {
  let isLogin = localStorage.getItem("P4_userId")
  if (isLogin) {
    next('/my')
    Toast({
      message: "您已登录",
      position: "bottom"
    })
  } else {
    next();
  }
}


const routes = [
  // & 根目录重定向
  {
    path: '/',
    redirect: "/home"
  },
  // & 主页
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/HomeView.vue'),
    beforeEnter,
  },
  // & 分类页
  {
    path: '/category',
    name: 'category',
    meta: {keepAlive: true} ,
    component: () => import('../views/categoryView.vue')
  },
  // & 购物车
  {
    path: '/shopCar',
    name: 'shopCar',
    component: () => import('../views/shopCarView.vue')
  },
  // & 个人页
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/myView.vue')
  },
  // & 登录页
  {
    path: '/Login',
    name: 'Login',
    component: () => import('../views/LoginView.vue'),
    beforeEnter: isLogin
  },
  // & 注册页
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/registerView.vue'),
    beforeEnter: isLogin
  },
  // & 钱包
  {
    path: '/Wallet',
    name: 'Wallet',
    component: () => import('../views/WalletView.vue')
  },
  // & 详情页
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/detailsView.vue'),
 
  },
  // & 订单详情页
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/orderView.vue')
  },
  // & 个人订单页
  {
    path: '/myOrder',
    name: 'myOrder',
    component: () => import('../views/myOrderView.vue')
  },
  // & 搜索页
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/searchView.vue')
  },
  // & 地址管理页
  {
    path: '/address',
    name: 'address',
    component: () => import('../views/addressView.vue')
  },
  // & 收藏页
  {
    path: '/collect',
    name: 'collect',
      component: () => import('../views/collectView.vue')
  },
  // & 账号管理页
  {
    path: '/account',
    name: 'account',
    component: () => import('../views/accountView.vue')
  },
  // & 支付页
  {
    path: '/pay',
    name: 'pay',
    component: () => import('../views/payView.vue')
  },

   // & 联系客服页
   {
    path: '/service',
    name: 'service',
    component: () => import('../views/serviceView.vue')
  },
    // & 充值钱包
    {
      path: '/recharge',
      name: 'recharge',
      component: () => import('../views/rechargeView.vue')
    },
]


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
  linkActiveClass: "con"
})


export default router
